<?php
//STILL UNDER CONSTRUCTION.
//This controller handles the messages.

session_start();
/**
 * Description of messages
 *
 * @author bilde
 */
class messages extends CI_Controller
{

	function  __construct()
        {
           parent::__construct();

            //Logged_in control
            if (!$this->session->userdata('is_logged_in'))
            {
                redirect('/welcome/index','refresh');
            }

	}

        //Creates inbox-view which in the same, that index creates.
	function inbox()
        {
		redirect('messages/index', 'refresh');
	}

        //Creates message-view for a single meassage($id).
	function view_message($id){
		$msg = $this->model_messages->get_message($id);
		$data['title'] = $msg->subject;
		$data['main_content'] = 'msg/view_message';
		$data['user'] = $this->session->userdata('id');
		$data['msg'] = $msg;
		$data['usernames'] = $this->model_users->getAllUsernames();
		$this->load->vars($data);
		$this->load->view('includes/template');
	}

        //Load inbox-view.
	function index(){
		$data['title'] = 'Your Inbox';
		$data['main_content'] = 'msg/inbox';
		$data['user'] = $this->session->userdata('id');
		$data['messages'] = $this->model_messages->list_messages_to($this->session->userdata('id'));
		$data['usernames'] = $this->model_users->getAllUsernames();
		$this->load->vars($data);
		$this->load->view('includes/template');
	}

        //Load sent-view
	function sent(){
		$data['title'] = 'Sent Messages';
		$data['main_content'] = 'msg/sent';
		$data['user'] = $this->session->userdata('id');
		$data['messages'] = $this->model_messages->list_messages_from($this->session->userdata('id'));
		$data['usernames'] = $this->model_users->getAllUsernames();
		$this->load->vars($data);
		$this->load->view('includes/template');
	}

        //Load archive-view
	function archive(){
		$data['title'] = 'Your Archives';
		$data['main_content'] = 'msg/archive';
		$data['user'] = $this->session->userdata('id');
		$data['messages'] = $this->model_messages->list_messages_to($this->session->userdata('id'),'archived');
		$data['usernames'] = $this->model_users->getAllUsernames();
		$this->load->vars($data);
		$this->load->view('includes/template');
	}

        //load the compose-view, and list users to write to.
	function compose(){
		$data['title'] = 'Compose Message';
		$data['main_content'] = 'msg/compose';
		$data['user'] = $this->session->userdata('id');
		$data['usernames'] = $this->model_users->getAllUsernames();
		$this->load->vars($data);
		$this->load->view('includes/template');
	}

        //load respond view, and passing the original message as parameter
	function respond($id){
		$data['title'] = 'Respond';
		$data['respondid'] = $id;
		$data['message'] = $this->model_messages->get_message($id);
		$data['main_content'] = 'msg/respond';
		$data['user'] = $this->session->userdata('id');
		$data['usernames'] = $this->model_users->getAllUsernames();
		$this->load->vars($data);
		$this->load->view('includes/template');
	}

        //Send message
	function send_message(){
		$this->model_messages->send_message($this->session->userdata('id'));
		redirect('messages/index','refresh');

	}

        //Moves message to archive-folder
	function archive_message($id){
		$this->model_messages->move_message($id);
		redirect('messages/index','refresh');

	}

        //Moves message to inbox-folder
	function inbox_message($id){
		$this->model_messages->move_message($id,'inbox');
		redirect('messages/index','refresh');

	}
}
?>
